home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / TextureByReference / ImageOps.java.z / ImageOps.java
Encoding:
Java Source  |  2003-08-08  |  6.0 KB  |  169 lines

  1. /*
  2.  *    @(#)ImageOps.java 1.6 02/04/01 15:04:07
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.awt.*;
  41. import java.awt.image.*;
  42. import java.awt.color.*;
  43.  
  44. // some useful, static image operations
  45.  
  46. public class ImageOps {
  47.  
  48.   // flip the image
  49.   public static void flipImage(BufferedImage bImage) {
  50.     int width = bImage.getWidth();
  51.     int height = bImage.getHeight();
  52.     int[] rgbArray = new int[width*height];
  53.     bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
  54.     int[] tempArray = new int[width*height];
  55.     int y2 = 0;
  56.     for (int y = height-1; y >= 0; y--) {
  57.       for (int x = 0; x < width; x++) {
  58.     tempArray[y2*width+x] = rgbArray[y*width+x];
  59.       }
  60.       y2++;
  61.     }
  62.     bImage.setRGB(0, 0, width, height, tempArray, 0, width);
  63.   }
  64.  
  65.  
  66.   // convert the image to a specified BufferedImage type and return it
  67.   public static BufferedImage convertImage(BufferedImage bImage, int type) {
  68.     int width = bImage.getWidth();
  69.     int height = bImage.getHeight();
  70.     BufferedImage newImage = new BufferedImage(width, height, type);
  71.     int[] rgbArray = new int[width*height];
  72.     bImage.getRGB(0, 0, width, height, rgbArray, 0, width);
  73.     newImage.setRGB(0, 0, width, height, rgbArray, 0, width);
  74.     return newImage;
  75.   }
  76.  
  77.   // print out some of the types of BufferedImages
  78.   static void printType(BufferedImage bImage) {
  79.     int type = bImage.getType();
  80.     if (type == BufferedImage.TYPE_4BYTE_ABGR) {
  81.       System.out.println("TYPE_4BYTE_ABGR");
  82.     }
  83.     else if (type == BufferedImage.TYPE_INT_ARGB) {
  84.       System.out.println("TYPE_INT_ARGB");
  85.     }
  86.     else if (type == BufferedImage.TYPE_3BYTE_BGR) {
  87.       System.out.println("TYPE_3BYTE_BGR");
  88.     }
  89.     else if (type == BufferedImage.TYPE_CUSTOM) {
  90.       System.out.println("TYPE_CUSTOM");
  91.     }
  92.     else System.out.println(type);
  93.   }
  94.  
  95.   public static BufferedImage convertToCustomRGBA(BufferedImage bImage) {
  96.     if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) {
  97.       ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB);
  98.     }
  99.  
  100.     int width = bImage.getWidth();
  101.     int height = bImage.getHeight();
  102.  
  103.     ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
  104.     int[] nBits = {8, 8, 8, 8};
  105.     ColorModel cm = new ComponentColorModel(cs, nBits, true, false,
  106.                         Transparency.OPAQUE, 0);
  107.     int[] bandOffset = {0, 1, 2, 3};
  108.  
  109.     WritableRaster newRaster =
  110.       Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height,
  111.                      width*4, 4, bandOffset, null);
  112.     byte[] byteData = ((DataBufferByte)newRaster.getDataBuffer()).getData();
  113.     Raster origRaster = bImage.getData();
  114.     int[] pixel = new int[4];
  115.     int k = 0;
  116.     for (int j = 0; j < height; j++) {
  117.       for (int i = 0; i < width; i++) {
  118.     pixel = origRaster.getPixel(i, j, pixel);
  119.     byteData[k++] = (byte)(pixel[0]);
  120.     byteData[k++] = (byte)(pixel[1]);
  121.     byteData[k++] = (byte)(pixel[2]);
  122.     byteData[k++] = (byte)(pixel[3]);
  123.       }
  124.     }
  125.     BufferedImage newImage = new BufferedImage(cm, newRaster, false, null);
  126. //     if (newImage.getType() == BufferedImage.TYPE_CUSTOM) {
  127. //       System.out.println("Type is custom");
  128. //     }
  129.     return newImage;
  130.   }
  131.  
  132.   public static BufferedImage convertToCustomRGB(BufferedImage bImage) {
  133.     if (bImage.getType() != BufferedImage.TYPE_INT_ARGB) {
  134.       ImageOps.convertImage(bImage, BufferedImage.TYPE_INT_ARGB);
  135.     }
  136.  
  137.     int width = bImage.getWidth();
  138.     int height = bImage.getHeight();
  139.  
  140.     ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
  141.     int[] nBits = {8, 8, 8};
  142.     ColorModel cm = new ComponentColorModel(cs, nBits, false, false, 
  143.                        Transparency.OPAQUE, 0);
  144.     int[] bandOffset = {0, 1, 2};
  145.  
  146.     WritableRaster newRaster =
  147.       Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, 
  148.                      width*3, 3, bandOffset, null);
  149.     byte[] byteData = ((DataBufferByte)newRaster.getDataBuffer()).getData();
  150.     Raster origRaster = bImage.getData();
  151.     int[] pixel = new int[4];
  152.     int k = 0;
  153.     for (int j = 0; j < height; j++) {
  154.       for (int i = 0; i < width; i++) {
  155.     pixel = origRaster.getPixel(i, j, pixel);
  156.     byteData[k++] = (byte)(pixel[0]);
  157.     byteData[k++] = (byte)(pixel[1]);
  158.     byteData[k++] = (byte)(pixel[2]);
  159.       }
  160.     }
  161.     BufferedImage newImage = new BufferedImage(cm, newRaster, false, null);
  162. //     if (newImage.getType() == BufferedImage.TYPE_CUSTOM) {
  163. //       System.out.println("Type is custom");
  164. //     }
  165.     return newImage;
  166.   }
  167. }
  168.  
  169.